package com.wxfh.qimeng;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridLayout;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private GridLayout mainLayout;
    private Button[] numberButtons;
    private FrameLayout container;
    private int mScreenWidth;
    private int mScreenHeight;
    private int normalSize = 180;
    private int bigSize = 380;
    private MediaPlayer[] mediaPlayers;
    private Random random = new Random();

    private int currentTrack = 0; // 当前播放的音轨索引

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        container = findViewById(R.id.container);
        findViewById(R.id.addButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRandomView();
            }
        });

        mainLayout = findViewById(R.id.mainLayout);
        mainLayout.setColumnCount(3);
        mainLayout.setRowCount(3);
        numberButtons = new Button[9];
        mediaPlayers = new MediaPlayer[9];
        // 动态计算屏幕宽度和高度
        mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = getResources().getDisplayMetrics().heightPixels;

        for (int i = 0; i < 9; i++) {
            final int number = i + 1;

            // 假设您的音频文件为 R.raw.track_1 到 R.raw.track_9
            int resourceId = getResources().getIdentifier("a" + number, "raw", getPackageName());
            mediaPlayers[i] = MediaPlayer.create(this, resourceId);

            numberButtons[i] = new Button(this);
            numberButtons[i].setText(String.valueOf(number));
            numberButtons[i].setBackgroundColor(getColor(R.color.white));
            numberButtons[i].setTextColor(getColor(R.color.black));
            numberButtons[i].setTextSize(normalSize);
            numberButtons[i].setOnClickListener(v -> handleButtonClick((Button) v, number));


            int buttonWidth = mScreenWidth / 3;
            int buttonHeight = mScreenHeight / 3;

            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
            params.width = buttonWidth;
            params.height = buttonHeight;
            int row = i / 3;
            int col = i % 3;
            params.rowSpec = GridLayout.spec(row);
            params.columnSpec = GridLayout.spec(col);
            numberButtons[i].setLayoutParams(params);

            numberButtons[i].setTag(new int[] {i / 3, i % 3});

            mainLayout.addView(numberButtons[i]);
        }
    }

    private void handleButtonClick(Button v, int number) {
        showFullScreenView(v, number);
    }

    private void showFullScreenView(View clickedView, int number) {
        // 创建全屏的 View
        Button fullScreenView = new Button(this);
        fullScreenView.setOnClickListener(v -> {
            hideFullScreenView();
        });
        fullScreenView.setText(String.valueOf(number));
        fullScreenView.setTextColor(getColor(R.color.black));
        fullScreenView.setTextSize(bigSize);
        fullScreenView.setBackgroundColor(getColor(R.color.white)); // 设置背景颜色

        // 添加到容器中
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            mScreenWidth,
            mScreenHeight
        );
        container.addView(fullScreenView, params);
        readNumber(number);
    }

    private void showRandomView() {
        // 创建全屏的 View
        Button fullScreenView = new Button(this);
        int number = random.nextInt(9) + 1;
        fullScreenView.setOnClickListener(v -> {
            hideFullScreenView();
//            isFullScreen = false;
        });
        fullScreenView.setText(String.valueOf(number));
        fullScreenView.setTextColor(getColor(R.color.black));
        fullScreenView.setTextSize(bigSize);
        fullScreenView.setBackgroundColor(getColor(R.color.white)); // 设置背景颜色

        // 添加到容器中
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            mScreenWidth,
            mScreenHeight
        );
        if (container.getChildCount() > 1) {
            container.removeViewAt(container.getChildCount() - 1);
        }
        container.addView(fullScreenView, params);
        readNumber(number);
    }

    private void hideFullScreenView() {
        // 移除全屏 View
        if (container.getChildCount() > 1) {
            container.removeViewAt(container.getChildCount() - 1);
        }
        stopCurrentTrack();
    }

    private void readNumber(int number) {
        stopCurrentTrack();
        currentTrack = number - 1;
        mediaPlayers[currentTrack].start();
        mediaPlayers[currentTrack].seekTo(350);
    }

    private void stopCurrentTrack() {
        if (currentTrack > 0 && mediaPlayers[currentTrack] != null && mediaPlayers[currentTrack].isPlaying()) {
            mediaPlayers[currentTrack].pause(); // 暂停播放
            mediaPlayers[currentTrack].seekTo(0); // 回到开始位置
        }
    }

    @Override
    protected void onDestroy() {
        // 释放 MediaPlayer 资源
        for (MediaPlayer player : mediaPlayers) {
            if (player != null) {
                player.release();
            }
        }
        super.onDestroy();
    }
}